<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>简易的计算器</title>
</head>
<body>
<form action="" name="myform">
    <input type="text" id="num1">

    <select name="symbol">
        <option value="add">+</option>
        <option value="reduce">-</option>
        <option value="multiplication">*</option>
        <option value="division">/</option>
    </select>

    <input type="text" id="num2">
    <button type="button" onclick="f()">=</button>
    <input type="text" id="result">
</form>

<script>
    function f() {
        const num1 = Number(document.getElementById("num1").value);
        const num2 = Number(document.getElementById("num2").value);
        const symbol = document.myform.symbol.value;
        let result = "";
        // 判断用户输入的数字是否为空
        if (num1 === null || num2 === null){
            alert("请输入数字后再进行计算。")
        }
        // 对数据进行运算操作
        if (symbol === "add"){
            result = num1 + num2;
        }else if(symbol === "reduce"){
            result = num1 - num2;
        }else if (symbol === "multiplication"){
            result = num1 * num2;
        }else{
            if (num2 === 0){
                alert("0 不能作为除数。")
            }else{
                result = num1 / num2;
            }
        }
        document.getElementById("result").value = result;
        console.log(result)
    }
</script>
</body>
</html>
